package com.meiyou.controller;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.meiyou.pojo.MeiyouPtjls;
import com.meiyou.pojo.MeiyouYuehui;
import com.meiyou.service.YueHuiService;
import com.meiyou.utils.Msg;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value="测试控制器",tags="测试")
public class TestController {
	@Resource
	private YueHuiService yueHuiServiceImpl;
	
	@RequestMapping(value = "/insertYuehui", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "确认发布约会")
	public Msg Fabu(@RequestParam("mfile") MultipartFile[] mfile,String ySex,String yAddress,String yDate,String yContext,
			Integer yMeijin,String yPingtai,HttpServletRequest request,String uCount){
		
				//这里mfile可以接受前台的文件，可以是一个，也可以是多个，这正是MultipartFile强大的地方。
				HashMap<String, String> map=new HashMap<String, String>();
				//建一个Map便于返回json
				String filePath =null;
				String fileName[]=new String[mfile.length];
				Date date=new Date();
				//根据传递文件个数返回文件（{msg1:"sss.png",msg2:"aa.jpg"}
				String img = "[";
				if (mfile != null && mfile.length > 0) {
				//判断使用有文件上传
					for (int i = 0; i < mfile.length; i++) {
						//循环上传（多文件上传）
						try {
							Calendar cale = null;	
							cale = Calendar.getInstance();
							int year = cale.get(Calendar.YEAR);
							int month = cale.get(Calendar.MONTH) + 1;
							int day = cale.get(Calendar.DATE);
							//给图片重命名加上日期前缀
							filePath= request.getSession().getServletContext().getRealPath("/") ;
							//获取服务器img文件夹地址（这里指的是local:8080/项目名/img）
							fileName[i]= "/imgs/" + year + ""+ month + "" + day + "_" + mfile[i].getOriginalFilename();
							//重命名图片。
							String type = fileName[i].substring(fileName[i].lastIndexOf(".")+1, fileName[i].length());
							//截取文件后缀判断是否是一张图片
							if((type.equals("png"))||(type.equals("jpg"))||(type.equals("jpeg"))){
								int length = mfile[i].getBytes().length;
								if (!((length / 1000) > 5120)) {
									//判断图片地址
									map.put("msg"+i, fileName[i]);
									//将图片输出到制定的文件夹目录中
									mfile[i].transferTo(new File(filePath,fileName[i]));
									if(i==mfile.length-1){
										img +="\""+fileName[i]+"\"]";
									}else{
										img +="\""+fileName[i]+"\",";
									}
									
								}else{
									map.put("msg", "图片过大");
								}}else{
								map.put("msg", "只能上传图片啊亲!");}
						} catch (Exception e) {
							e.printStackTrace();
							map.put("msg", "上传失败");}}} else {
					map.put("msg", "请选择文件");}
				MeiyouYuehui meiyouYuehui = new MeiyouYuehui();
				meiyouYuehui.setyTime(date);
				meiyouYuehui.setyState("已发布");
				meiyouYuehui.setY_Assess(yAddress);
				meiyouYuehui.setyContext(yContext);
				meiyouYuehui.setyMeijin(yMeijin);
				meiyouYuehui.setyPingtai(yPingtai);
				meiyouYuehui.setySex(ySex);
				meiyouYuehui.setyDate(yDate);
				meiyouYuehui.setyUCount(uCount);
				yueHuiServiceImpl.insYueHui(meiyouYuehui);
				
		return Msg.success();
		
	}
	
}
